package com.bysj.money_diary;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FindPasswordActivity extends Activity implements OnClickListener{
	private static final String TAG = null;
	private Button findpaw_submit;
	private EditText findpaw_name,findpaw_pho,findpaw_password_new,findpaw_password_new1,findpaw_ans;
	Data dataHelper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_findpassword);
		dataHelper = new Data(this);
		initView();
	}
	private void initView() {
		findpaw_name=(EditText)findViewById(R.id.findpaw_name);
		findpaw_submit=(Button)findViewById(R.id.findpaw_submit);
		findpaw_pho=(EditText)findViewById(R.id.findpaw_pho);
		findpaw_ans=(EditText)findViewById(R.id.findpaw_ans);
		findpaw_password_new=(EditText)findViewById(R.id.findpaw_password);
		findpaw_password_new1=(EditText)findViewById(R.id.findpaw_password1);
		
		
		findpaw_submit.setOnClickListener(this);
		
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.findpaw_submit:
			resetpwd_check();
			break;
		default:
		
		}
	}
	private void resetpwd_check() {
		if (isUserNameAndPwdValid()){
			String userName = findpaw_name.getText().toString().trim();    
            String userPwd_new = findpaw_password_new.getText().toString().trim();  
            String userPwdCheck = findpaw_password_new1.getText().toString().trim();  
            String userPho = findpaw_pho.getText().toString().trim();
            String userAns = findpaw_ans.getText().toString().trim(); 
            
            if(!dataHelper.haveUserInfo(userName)){  
                Toast.makeText(this, getString(R.string.name_not_exist, userName),Toast.LENGTH_SHORT).show();  
                return ;  
            } 
            int result=dataHelper.findUserByNameAndPhoAndAns(userName, userPho,userAns); 
            if(result==1){                                              
                if(userPwd_new.equals(userPwdCheck)==false){            
                    Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();  
                    return ;  
                } else {  
        			User user = new User();//实例化
        			int flag = dataHelper.UpdateUserInfo(user,userPwd_new, userName);     
        			Log.i(TAG,"UpdateUserInfo , flag="+flag); 
                    if (flag < 1) {  
                        Toast.makeText(this, getString(R.string.findpwd_fail),Toast.LENGTH_SHORT).show();  
                    }else{  
                        Toast.makeText(this, getString(R.string.findpwd_success),Toast.LENGTH_SHORT).show();    
                        Intent intent1 = new Intent();
        				intent1.setClass(FindPasswordActivity.this, LoginActivity.class);  
        				startActivity(intent1);
        				finish();  
    	                dataHelper.CloseUser(user);
                    }  
                }  
            }else if(result==0){                                         
                Toast.makeText(this, getString(R.string.check_fail),Toast.LENGTH_SHORT).show();  
                return;  
            }
		}
		
	}
	private boolean isUserNameAndPwdValid() { 
        if (findpaw_name.getText().toString().trim().equals("")) {  
            Toast.makeText(this, getString(R.string.account_empty),Toast.LENGTH_SHORT).show();  
            return false;  
        }else if (findpaw_pho.getText().toString().trim().equals("")){
		 Toast.makeText(this, getString(R.string.pho_check_empty),Toast.LENGTH_SHORT).show();  
         return false;
        }else if (findpaw_ans.getText().toString().trim().equals("")){
		 Toast.makeText(this, getString(R.string.ans_check_empty),Toast.LENGTH_SHORT).show();  
         return false;
        } else if (findpaw_password_new.getText().toString().trim().equals("")) {  
            Toast.makeText(this, getString(R.string.pwd_new_empty),Toast.LENGTH_SHORT).show();  
            return false;  
        }else if(findpaw_password_new1.getText().toString().trim().equals("")) {  
            Toast.makeText(this, getString(R.string.pwd_check_empty),Toast.LENGTH_SHORT).show();  
            return false;  
        }  
        return true;
	}

	
}
